<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 18/3/26
 * Time: 16:22
 */

namespace App\Http\Api\Admin;


use App\Http\API\ApiController;
use App\Models\Account;
use App\Services\Admin\UserAccountService;
use App\Services\Admin\UserService;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class DashboardApiController extends ApiController
{
    protected $service;
    public function __construct(UserAccountService $service)
    {
        $this->service = $service;
    }

    public function user(Request $request,UserService $service){
        $type = $request->input('type','all');
        $count = $service->count($type);
        return $this->success(1,['count'=>number_format($count,0)]);
    }
    public function account(Request $request,UserAccountService $service){
        $type = $request->input('type','all');
        $day_type = $request->input('day_type','all');
        $count = $service->count($type,$day_type);
        if($type=='cost')$count = $count*(-1);
        return $this->success(1,['count'=>number_format($count,2)]);
    }
    public function chart_user(Request $request,UserService $service){
        $type = $request->input('type','day');

        $data = $service->getCharts($type);

        if($data){
            return $this->success(1,[
                'datas'  =>  $data,
                'xAxis' =>  $service->xAxis($type)
            ]);
        }else{
            return $this->fail(-1);
        }
    }
    public function chart_account(Request $request,UserAccountService $service){
        $type = $request->input('type','day');

        $data1 = $service->getCharts($type);
        $data2 = $service->getCharts($type,Account::TYPE_COST);
        if($data1&&$data2){
            return $this->success(1,[
                'datas'  =>  [$data1,$data2],
                'xAxis' =>  $service->xAxis($type)
            ]);
        }else{
            return $this->fail(-1);
        }
    }
}